package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.module;

import com.iwhalecloud.bss.kite.client.api.scene.operate.IOperateSceneProdInstService;
import com.iwhalecloud.bss.kite.client.dto.inst.InstAttrDTO;
import com.iwhalecloud.bss.kite.client.vo.scene.ModuleVO;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.SceneNewOfferSeneVO;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.TransferPlanModuleVO;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.TransferPlanVO;
import com.iwhalecloud.bss.kite.dataservice.cache.ProductCache;
import com.iwhalecloud.bss.kite.dataservice.entity.spec.Product;
import com.iwhalecloud.bss.kite.dataservice.mapper.spec.OfferDetailMapper;
import com.iwhalecloud.bss.kite.dataservice.mapper.spec.ProductMapper;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.service.util.CopyToDtoUtil;
import com.iwhalecloud.bss.kite.web.context.KiteWebContext;
import com.iwhalecloud.bss.kite.web.service.accept.bo.AcceptContext;
import com.iwhalecloud.bss.kite.web.service.accept.bo.SceneModule;
import com.iwhalecloud.bss.kite.web.service.accept.handler.module.AbstractModuleHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

@Service
public class TransferPlanModuleHandler extends AbstractModuleHandler {
    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Autowired
    private IOperateSceneProdInstService operateSceneProdInstService;

    @Resource
    private OfferDetailMapper offerDetailMapper;

    @Resource
    private ProductMapper productMapper;

    private static final String FIELD_NAME_200002037 = "200002037";

    @Override
    public List<ModuleVO> execute(AcceptContext context, SceneModule sceneModule) {
        String sceneInstId = context.getAcceptRequest().getSceneInstId();
        String custId = KiteWebContext.getContext().getCustId();
        TransferPlanModuleVO TransferPlanModuleVO = new TransferPlanModuleVO();
        List<TransferPlanVO> transferPlanList = new ArrayList<TransferPlanVO>();
        String offerInstId = querySceneInstService.getSceneInst(context.getAcceptRequest().getSceneInstId())
            .getRootInstId();

        List<KiteProdInst> kiteProdInstList = querySceneInstService.listProdInsts(sceneInstId);

        if (KiteListUtils.isEmpty(kiteProdInstList)) {
            return Collections.singletonList(TransferPlanModuleVO);
        }

        transferPlanList = this.getKiteProdInstList(kiteProdInstList);
        if (KiteListUtils.isEmpty(transferPlanList)) {
            /*List<OfferDetail> offerDetails = offerDetailMapper
                .listOfferDetails(context.getAcceptRequest().getOfferId());
            TransferPlanVO transferPlanVO = new TransferPlanVO();
            for (OfferDetail offerDetail : offerDetails) {
                Product product = productMapper.selectinfoByProdId(offerDetail.getObjId());
                if (KiteStringUtils.isEqual(KeyConsts.PROD_FUNC_TYPE_105, product.getProdFuncType())) {
                    ProdInstDTO prodInstDTO = operateSceneProdInstService.addProdRole(sceneInstId,
                        ContextUtil.getUserId().toString(), custId, offerInstId, product.getProdId(),
                        kiteProdInstList.get(0).getRoleId(), "");
                    KiteProdInst kiteProdInst = querySceneInstService.getProdInst(custId, prodInstDTO.getProdInstId());
                    List<InstAttrDTO> kiteAttrList = CopyToDtoUtil.transInstAttrDtoList(kiteProdInst.listAttrs());
                    //传输计划的属性在此处全置为D显示，单商品属性有部分与传输计划属性一致
                    kiteAttrList.forEach(instAttrDTO -> {
                        instAttrDTO.setShowType(LocalKeyConsts.KITE_ATTR_CONFIG_SHOW_TYPE_D);
                    });
                    transferPlanVO.setPlanAttrs(kiteAttrList);
                    transferPlanVO.setTransferTaskInstId(prodInstDTO.getProdInstId());
                    transferPlanList.add(transferPlanVO);
                }
            }*/
        }
        TransferPlanModuleVO.setTransferPlans(transferPlanList);
        TransferPlanModuleVO.setBillTypeAttr(this.getBillTypeAttr(kiteProdInstList));
        SceneNewOfferSeneVO sceneAccountInfoVo = (SceneNewOfferSeneVO) context.getSceneVo();
        sceneAccountInfoVo.setTransferPlanModuleVO(TransferPlanModuleVO);
        return Collections.singletonList(TransferPlanModuleVO);
    }

    /**
     * 获取计费模式
     *
     * @param kiteProdInstList
     * @return
     */
    public InstAttrDTO getBillTypeAttr(List<KiteProdInst> kiteProdInstList) {
        if (KiteListUtils.isNotEmpty(kiteProdInstList)) {
            for (KiteProdInst kiteProdInst : kiteProdInstList) {
                List<InstAttrDTO> instAttrDTOs = CopyToDtoUtil.transInstAttrDtoList(kiteProdInst.listAttrs());
                for (InstAttrDTO InstAttrDTO : instAttrDTOs) {
                    if (KiteStringUtils.isEqual(FIELD_NAME_200002037, InstAttrDTO.getFieldName())) {
                        return InstAttrDTO;
                    }
                }
            }
        }
        return null;
    }

    public List<TransferPlanVO> getKiteProdInstList(List<KiteProdInst> kiteProdInstList) {
        List<TransferPlanVO> transferPlanList = new ArrayList<TransferPlanVO>();
        TransferPlanVO transferPlanVO = new TransferPlanVO();
        for (KiteProdInst kiteProdInst : kiteProdInstList) {
            Product product = ProductCache.get(kiteProdInst.getProdId());
            if (Objects.isNull(product)) {
                product = productMapper.selectinfoByProdId(kiteProdInst.getProdId());
            }
            if (KiteStringUtils.isEqual(KeyConsts.PROD_FUNC_TYPE_105, product.getProdFuncType())) {
                transferPlanVO.setPlanAttrs(CopyToDtoUtil.transInstAttrDtoList(kiteProdInst.listAttrs()));
                transferPlanVO.setTransferTaskInstId(kiteProdInst.getProdInstId());
                transferPlanList.add(transferPlanVO);
            }
        }
        return transferPlanList;
    }

    @Override
    public List<ModuleVO> previewSceneVo(AcceptContext context, SceneModule sceneModule) {
        List<ModuleVO> moduleVos = this.execute(context, sceneModule);
        return moduleVos;
    }
}
